#include <iostream>
using namespace std;

const int N = 1010;
int dp[N][N];
//dp[i][j] : s1 0->i 和 s2 0->j 最多的公共子序列
//if(s1[i] == s2[j])  -> dp[i][j] = dp[i - 1][j - 1] + 1
//if(s1[i] != s2[j])  -> dp[i][j] = max(dp[i-1][j],dp[i][j-1]);

int main() {
    int n,m;
    cin >> n >> m;
    string s1,s2;
    cin >> s1 >> s2;
    s1 = " " + s1,s2 = " " + s2;
    for(int i = 1;i <= n;i++)
    {
        for(int j = 1;j <= m;j++)
        {
            dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            if(s1[i] == s2[j])
            {
                dp[i][j] = max(dp[i][j],dp[i-1][j-1] + 1);
            }
        }
    }
    cout << dp[n][m] << endl;

    return 0;
}
// 64 位输出请用 printf("%lld")